#!/usr/bin/env node


const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === '/') {
    // 设置响应头
    res.writeHead(200, {
      'Content-Type': 'text/html',
      'Content-Length': Buffer.byteLength('<h1>Hello World!</h1>')  // 计算响应体字节长度
    });
    // 发送响应体
    res.end('<h1>Hello World!</h1>');
  } else {
    // 设置响应头
    res.writeHead(404, {
      'Content-Type': 'text/plain'
    });
    // 发送响应体
    res.end('Resource not found!');
  }
});

server.listen(8080, () => {
  console.log('Server running at http://localhost:8080/');
});
